package com.example.monthmoni.filter;

import jakarta.annotation.Resource;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;

/**
 * @Author 宋伟宁
 * @Date 2023/11/28
 * @Version 1.0
 **/
@Component
public class LoginProcessFilter extends OncePerRequestFilter {

    @Resource
    private RedisTemplate redisTemplate;
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String code = (String)this.redisTemplate.opsForValue().get("mycode");
        String chkcode = request.getParameter("chkcode");
        if(request.getRequestURI().equals("/login") && request.getMethod().equals("POST")) {
            if (code.equals(chkcode)) {
                filterChain.doFilter(request, response);
            } else {
                response.sendRedirect("/login");
            }
        }else{
            filterChain.doFilter(request,response);
        }
    }
}
